package com.meitianhui.convenitenbuy.main.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.contract.MainContract;
import com.meitianhui.convenitenbuy.main.http.MainApiDataSource;
import com.meitianhui.convenitenbuy.main.model.bean.CollectedStores;
import com.meitianhui.convenitenbuy.main.model.bean.UpdateBean;
import com.meitianhui.convenitenbuy.main.model.bean.VersionInfo;
import com.meitianhui.convenitenbuy.main.ui.MainActivity;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.BannerBean;
import com.meitianhui.convenitenbuy.module.wallet.model.RedPacketBean;
import com.meitianhui.convenitenbuy.util.SystemUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/10/28.
 */
public class MainPresenter implements MainContract.Presenter {

    private MainActivity context;
    private final MainContract.View view;

    @Inject
    public MainPresenter(MainContract.View view, MainActivity context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void getAd() {
        Map<String, String> params = new HashMap<>();
        params.put("category", "c_app_popup");
        String json = new Gson().toJson(params);

        HomeApiDataSource.getInstance()
                .getBanner(InfoProvider.getAppToken(App.getInstance()),
                        "gdAppAdvert.app.gdAppAdvertFind", json)
                .subscribe(new ResponseObserver<BaseResponse<BannerBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<BannerBean> response) {
                        view.getAdSuccess(response.data);
                    }
                });
    }

    @Override
    public void getVersion() {
        MainApiDataSource.getInstance()
                .getVersionInfo("com.meitianhui.convenitenbuy")
                .subscribe(new ResponseObserver<BaseResponse<VersionInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<VersionInfo> response) {
                        view.getVersionSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                    }
                });
    }

    @Override
    public void getUpdateInfo() {
        int versionCode = SystemUtils.getVersionCode(App.getInstance());
        Map<String, String> params = new HashMap<>();
        params.put("app_type", "2");
        params.put("version_no", String.valueOf(versionCode));
        String json = new Gson().toJson(params);

        MainApiDataSource.getInstance()
                .getUpdateInfo(InfoProvider.getAppToken(App.getInstance()),
                        "consumer.consumer.appVersionFind", json)
                .subscribe(new ResponseObserver<BaseResponse<UpdateBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<UpdateBean> response) {
                        view.getUpdateInfoSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                    }
                });
    }

    @Override
    public void getDefaultStores() {
        Map<String, String> params = new HashMap<>();
        params.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("is_llm_stores", "Y");
        String json = new Gson().toJson(params);

        MainApiDataSource.getInstance()
                .getDefaultStores(InfoProvider.getAppToken(App.getInstance()),
                        "stores.consumer.favoriteStoreList", json)
                .subscribe(new ResponseObserver<BaseResponse<CollectedStores>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<CollectedStores> response) {
                        view.getDefaultStoresSuccess(response.data);
                    }
                });
    }

    // 创建红包订单
    @Override
    public void getRedpacket(String redChange) {
        Map<String, String> params = new HashMap<>();
        params.put("to_member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("to_member_type_key", "consumer");
        params.put("to_member_info", InfoProvider.getUserInfo(App.getInstance()).getMobile());
        params.put("gift_value", redChange);

        MainApiDataSource.getInstance()
                .getRedPacket(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "gcActivity.consumer.gcActivityDetailCreate", new Gson().toJson(params))
                .subscribe(new ResponseObserver<BaseResponse<RedPacketBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<RedPacketBean> redPacketBean) {
                        view.showRedPacketSucc(redPacketBean.data.getDetail_id(), redPacketBean.data.getGift_value());
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.showRedPacketFail(msg);
                    }
                });
    }

    @Override
    public void openRedPacket(String detail_id, String gift_value) {
        Map<String, String> params = new HashMap<>();
        params.put("detail_id", detail_id);
        params.put("member_type_key", "consumer");
        params.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());

        MainApiDataSource.getInstance()
                .openRedPacket(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "gcActivity.consumer.gcActivityDetailOpen", new Gson().toJson(params))
                .subscribe(new ResponseObserver<BaseResponse>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse baseResponse) {
                        view.openRedPacketSucc(gift_value);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.showFail(msg);
                    }
                });
    }


    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
